﻿/*----------------------------------------------------------------
// Copyright (C) 2011 上海
// 版权所有。 
//
// 文件名：DataAccessInstockDetailed
// 文件功能描述：食材chu库明细数据访问层
//
// 
// 创建标识：2012-01-14 zhou
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbAdapter.sqldbAdapter;
using System.Data;
using System.Data.SqlClient;

namespace DAO.WaiGaoQiao
{

    public class DataAccessOutstockDetailed
    {

        /// <summary>
        /// 食材出库数量合计
        /// </summary>
        /// <returns></returns>
        public int GetOutstockDetailedCount(string Condition)
        {
            sqldb db = new sqldb();
            string query = "select count(1) from Con_Outstock_Detailed where 1=1 " + Condition;
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);

        }
        /// <summary>
        /// 食材出库总数
        /// </summary>
        /// <returns></returns>
        public int GetCountOutstockDetailed(string condition)
        {
            string query = "select count(1) from Con_Outstock_Detailed where 1=1";
            if (!string.IsNullOrEmpty(condition))
            {
                query += condition;
            }
            sqldb db = new sqldb();
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }
        /// <summary>
        /// 获取食材出库不分页
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public DataSet GetAllOutstockDetailed(string condition)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select ID,OutstockDt,Warehouse_ID,Group_Id,Goods_Id,Count,Price,Amount,"
                             + "LeadMan,RecordDateTime,OperatorID,OrderNo FROM Con_Outstock_Detailed Where 1=1 " + condition;
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 获取食材明细分页
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="pagesize"></param>
        /// <param name="startindex"></param>
        /// <returns></returns>
        public DataSet GetAllOutstockDetailed(string condition, int pagesize, int startindex)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "SELECT B.ID,B.OutstockDt,B.Warehouse_ID,B.Group_Id,B.Goods_Id,B.Count,B.Price,B.Amount,"
                    + "B.LeadMan,B.RecordDateTime,B.OperatorID,B.OrderNo "
                    + "FROM (select ROW_NUMBER() OVER(ORDER BY A.ID DESC) rownumber,A.ID,A.OutstockDt,A.Warehouse_ID,A.Group_Id,A.Goods_Id,A.Count,A.Price,A.Amount,"
                    + "A.LeadMan,A.RecordDateTime,A.OperatorID,A.OrderNo FROM Con_Outstock_Detailed A Where 1=1 " + condition + ") B "
                    + "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString();
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 获取一条食材出库信息
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="pagesize"></param>
        /// <param name="startindex"></param>
        /// <returns></returns>
        public DataRow GetOutstockDetailed(int Id)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select ID,OutstockDt,Warehouse_ID,Group_Id,Goods_Id,Count,Price,Amount,"
                    + "LeadMan,RecordDateTime,OperatorID,OrderNo FROM Con_Outstock_Detailed Where ID = " + Id;
                return db.ExecuteDataset(conn, CommandType.Text, query).Tables[0].Rows[0];
            }
        }


        /// <summary>
        /// 保存食材出库信息
        /// </summary>
        /// <param name="idpara"></param>
        /// <returns></returns>
        public int SaveOutstockDetailed(object[] idpara)
        {
            sqldb db = new sqldb();
            return db.ExecuteNonQuery(GetConntionString.conntionstring(), "Proc_ConOutstockDetailed", idpara);
        }

        /// <summary>
        /// 根据食材出库条件删除
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public int DeleteOutstockDetailed(int Id)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "Delete from Con_Outstock_Detailed where ID =  " + Id;
                return db.ExecuteNonQuery(conn, CommandType.Text, query);
            }
        }
        /// <summary>
        /// 食材出库批号
        /// </summary>
        /// <returns></returns>
        public string BuilderOrderNo()
        {
            sqldb db = new sqldb();
            string query = "Execute GetCon_Outstock_Detailed_OrderNo";
            object strorderno = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return strorderno.ToString();
        }
        /// <summary>
        /// 验证库存数量
        /// </summary>
        /// <param name="GID"></param>
        /// <returns></returns>
        public bool OutStockYzh(int GID, decimal Count, decimal Price, string AddEdit, decimal OldCount)
        {
            sqldb db = new sqldb();
            string query = string.Empty;
            query = "select STOCKS_COUNT from CON_Goods_Stocks_View where ID=" + GID.ToString() + " and PRICE=" + Price;
            object stockcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            if (AddEdit == "Add")
            {
                return Convert.ToDecimal(stockcount) >= Count ? true : false;
            }
            else
            {
                return Convert.ToDecimal(stockcount) + OldCount >= Count ? true : false;
            }
        }
    }
}
